package web.lbsproject.processaudit.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.text.DateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;

import lbsproject.processaudit.model.CheckTask;
import lbsproject.processaudit.manager.TaskManager ;
import web.lbsproject.processaudit.model.CheckTaskDefine4Web;
import lbsproject.processaudit.model.enumeration.TaskStatus;
import lbsproject.processaudit.model.CheckPoint ;
import lbsproject.processaudit.model.enumeration.TaskLevel;
import web.lbsproject.processaudit.model.DateFormats;

/**
 * Trigered by TaskManagement=>Real-time state of check tasks.
 * Servlet implementation class ShowCheckPointsServlet
 */
public class ListAllCheckTasks extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListAllCheckTasks() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,  response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          TaskManager taskManager = new TaskManager();
          ServletOutputStream out = response.getOutputStream();
          List<CheckTask> checkTasks = taskManager.getAllCheckTasksWithoutListItem();
          
          String str= new String("[");

          for( CheckTask ct: checkTasks ){
               str += "{\"Lng\":\"" + Double.toString(ct.getCheckPoint().getGpsJingdu()) + "\",";
               str += "\"Lat\":\"" + Double.toString(ct.getCheckPoint().getGpsWeidu()) + "\",";
               str += "\"Type\":\"" + ct.getName() + "\",";
               str += "\"Point\":\"" + ct.getCheckPoint().getName() + "\",";
               str += "\"Status\":\"" + ct.getTaskStatus().getDbID() + "\"," ;
               str += "\"StatusDesc\":\"" + ct.getTaskStatus().toChnString() + "\"," ;
               str += "\"CDate\":\"" + DateFormats.YMD_FORMAT.format(ct.getCreateDate()) + "\",";
               str += "\"ADate\":\"" + DateFormats.YMD_FORMAT.format(ct.getAssignDate()) + "\",";
               str += "\"FDate\":\"" + DateFormat.getDateInstance().format(ct.getFinishDate()) + "\",";
               str += "\"Level\":\"" + ct.getTaskLevel().getDbID() + "\",";
               str += "\"LevelDesc\":\"" + TaskLevel.valueOfDbID(ct.getTaskLevel().getDbID()) + "\",";
               str += "\"Result\":\"" + ct.getExecuteResult().getDbID() + "\",";
               str += "\"ResultDesc\":\"" + ct.getExecuteResult().toChnString() + "\",";
               str += "\"AStaff\":\"" + ct.getAssignStaff().getName() + "\",";
               str += "\"Worker\":\"" + ct.getExeWorker().getName() + "\",";
               str += "\"Region\":\"" + ct.getCheckPoint().getLocatedRegion().getName() + "\"},";
          }
          if(str.length() != 1)
        	  str = str.substring(0, str.length() - 1) ;
          str += "]" ;

          //request.setAttribute("strCheckTasks", str);
          //RequestDispatcher dispatcher = request.getRequestDispatcher("/taskManage.jsp");
          //dispatcher.forward(request, response);
          out.write(str.getBytes("UTF-8"));
          out.flush();
          out.close();
	}
}
